﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using myu.Data;

namespace Sar
{
    public partial class FormMain : Form
    {
        #region ctor
        public FormMain(String username, Int32 previlege)
        {
            InitializeComponent();
            this.Username = username;
            this.Previlege = previlege;
            this.Opr = new SqlDataOpr("SELECT * FROM Sar.dbo.Command ORDER BY CommandNO");
            this.Table = Opr.fetchTable();
        }
        public FormMain() : this(null, 0) { }
        #endregion


        #region field
        public String Username { get; set; }
        public Int32 Previlege { get; set; }
        public SqlDataOpr Opr { get; set; }
        public DataTable Table { get; set; }
        #endregion


        #region method
        private void MainForm_Load(object sender, EventArgs e)
        {
            dataGridViewCommand.DataSource = this.Table;
        }
        private void buttonSelect_Click(object sender, EventArgs e)
        {
            try
            {
                DataView view = new DataView(this.Table);
                view.RowFilter = this.textBoxSql.Text;
                this.dataGridViewFilter.DataSource = view;
                this.dataGridViewFilter.Columns.RemoveAt(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n"
                                + ex.Source + "\n"
                                + ex.StackTrace + "\n"
                                + ex.TargetSite.ToString() + "\n");
            }

        }
        private void buttonInsert_Click(object sender, EventArgs e)
        {
            FormDML formInsertUpdate = new FormDML(this, "Insert");
            formInsertUpdate.ShowDialog();
        }
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            FormDML formInsertUpdate = new FormDML(this, "Update");
            formInsertUpdate.ShowDialog();
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            FormDML formInsertUpdate = new FormDML(this, "Delete");
            formInsertUpdate.ShowDialog();
        }
        private void buttonExecute_Click(object sender, EventArgs e)
        {
            int[] numbers = {1, 1, 2, 3, 5, 8, 13, 21};
            var subset = from y in numbers where y > 3 select y;
            foreach (int i in subset)
                Console.WriteLine(i);

            DataRow[] rows = this.Table.Select("CommandName = '" + textBoxCommanName.Text + "'");
            //IEnumerable<DataRow> rows = from r in this.Table.AsEnumerable() where r["CommandName"] == textBoxCommanName.Text select r;
            if (rows.Length == 1)
            {
                Process process = new Process();
                process.StartInfo.FileName = @"C:\Windows\System32\Cmd.exe";
                process.StartInfo.Arguments = " /c \"" + rows[0]["CommandLine"].ToString() + "\"";
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.Start();
                return;
            }
        }
        private void buttonQuit_Click(object sender, EventArgs e)
        {
            this.Opr.Dispose();
            this.Dispose();
        }
        #endregion
    }
}
